import java.awt.Rectangle;

import me.w70.bot.Bot;
import me.w70.bot.account.Account;
import me.w70.bot.scripting.Random;
import me.w70.bot.wrappers.RSInterface;

public class AutoLogin extends Random {
	/*
	 * private static RSInterfaceChild clickHereToPlay =
	 * RSInterface.getChildInterface(378, 18);
	 */
	public int scriptFail;

	public String curScript;

	public String lastScript;

	public int password;

	int tries = 0;

	@Override
	public boolean canRun() {
		tries = 0;
		
		return !isLoggedIn() || getGameState() == 10;
	}

	public void clickLogin() {
		Rectangle pos = new Rectangle(300, 177, 164, 10);

		clickMouse((int) random(pos.getMinX(), pos.getMaxX() - 10),
				(int) random(pos.getMinY(), pos.getMaxY()), true);
	}

	@Override
	public String getScriptAuthor() {
		return "spooky";
	}

	@Override
	public String getScriptName() {
		return "Auto Login";
	}
	int type = 0;
	@Override
	public int loop() {
		for (int tries = 0; tries < random(7, 10); tries++) {
            if (isLoggedIn()) {
                break;
            }
            if (getInterfaceGroup(744) == null) {
                wait(5000);
                continue;
            }
            RSInterface loginMessage = getInterfaceGroup(744).getInterface(627);
            if (loginMessage == null) {
                wait(2000);
                continue;
            }
            if (loginMessage.textContainsIgnoreCase("update")) {
                Bot.addMessage(this, "There has been an update to the runescape client!");
                Bot.stopScripts();
                break;
            }
            if (loginMessage.textContainsIgnoreCase("disable")) {
                Bot.addMessage(this, "Your account is banned :(");
                Bot.stopScripts();
                break;
            }
            if (loginMessage.textContainsIgnoreCase("forgotten")) {
                Bot.addMessage(this, "Wrong password.");
                Bot.stopScripts();
                break;
            }
            if (getGameState() == 10) {
                int type = 0;
                for (int i = 0; i < 31; i++) {
                    if (getInterfaceGroup(744).getInterface(612).getText().endsWith("|")) {
                        type = 1;
                        break;
                    } else if (getInterfaceGroup(744).getInterface(618).getText().endsWith("|")) {
                        type = 2;
                        break;
                    }
                    wait(random(15, 20));
                }

                if (type == 0) {/*
                    final RSInterfaceGroup group = getInterfaceGroup(744);
                    if(group != null){
                        final RSInterface anInterface = group.getInterface(627);
                        if(anInterface != null){
                            anInterface.doClick();
                        }
                        else{
                            return -1;
                        }
                    }
                    else{
                        return -1;
                    }*/
                	clickLogin();
                }
                if (type == 1) {
                    String text = getInterfaceGroup(744).getInterface(612).getText();
                    if (text.endsWith("|")) {
                        text = text.substring(0, text.length() - 1);
                    }
                    int usernameWritten = text.length();
                    Account account = Bot.getCurrentAccount();
                    if (usernameWritten != 0 && account.getUsername().length() <= usernameWritten) {
                        sendText("", true);
                    } else if (usernameWritten > 0) {
                        for (int i = 0; i <= usernameWritten; i++) {
                            sendText("\b", false);
                            wait(random(50, 100));
                        }
                    }
                    sendText(account.getUsername(), true);
                }
                if (type == 2) {
                    String text = getInterfaceGroup(744).getInterface(618).getText();
                    if (text.endsWith("|")) {
                        text = text.substring(0, text.length() - 1);
                    }
                    int passwordWritten = text.length();
                    Account account = Bot.getCurrentAccount();
                    if (passwordWritten != 0 && account.getPassword().length() <= passwordWritten) {
                        sendText("", true);
                    } else if (passwordWritten > 0) {
                        for (int i = 0; i <= passwordWritten; i++) {
                            sendText("\b", false);
                            wait(random(50, 100));
                        }
                    }
                    sendText(account.getPassword(), true);
                }
                wait(random(3000, 5000));
            } else {
                holdKey((char)27, random(200, 300)); // hold escape
                wait(random(200, 400));
            }

        }

		if (tries >= 7 && random(0, 4) == 2) {
			Bot.stopScripts();
		}
		return random(1000, 3000);
	}

	@Override
	public String getScriptCategory() {
		// TODO Auto-generated method stub
		return "Random";
	}

	@Override
	public String getScriptDescription() {
		// TODO Auto-generated method stub
		return "Logs in";
	}
}